package com.ssm.web;

import com.ssm.Entity.User;
import com.ssm.service.FileService;
import com.ssm.service.FolderService;
import com.ssm.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.Entity.Folder;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/folder")
public class FolderController {
    @Resource
    private FolderService folderService;

    @Resource
    private UserService userService;

    @Resource
    private FileService fileService;

    @RequestMapping("/addFolder")
    @ResponseBody
    public ModelAndView createFolder(Folder folder) {
        int result = 0;
        Double fileSize = 5120.00;
        folder.setFolderSize(fileSize);

        result = folderService.createFolder(folder); //调用接口中 定义的数据库操作方法
        if (result != 1) {
            throw new RuntimeException("添加文件夹失败");
        }
        ModelAndView mv = new ModelAndView("redirect:/folder/showFolder");
        return mv;
    }

    @RequestMapping("/showFolder")
    @ResponseBody
    public ModelAndView showFolder(HttpSession session1, ModelAndView modelAndView) {
        int userId = (int) session1.getAttribute("loggedIn");
        Folder folder = folderService.findFolderById(userId); //调用接口中 定义的数据库操作方法

        User user = userService.findUserById(userId); //调用接口中 定义的数据库操作方法

        com.ssm.Entity.File file = new com.ssm.Entity.File();
        file.setFolderId(folder.getFolderId());
        file.setUserId(userId);
        List<com.ssm.Entity.File> fileList = fileService.findFileByFolderId(file); //调用接口中 定义的数据库操作方法

        session1.setAttribute("user", user); //将查询到的文件夹信息存入session
        session1.setAttribute("fileList", fileList); //将查询到的文件夹信息存入session
        session1.setAttribute("folder", folder); //将查询到的文件夹信息存入session

        modelAndView.setViewName("showFolder");
        return modelAndView;
    }
}
